# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=libgpg-error
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.47
pkgrel=2
pkgdesc="Support library for libgcrypt (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://gnupg.org"
license=("LGPL")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc" "${MINGW_PACKAGE_PREFIX}-autotools")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs" "${MINGW_PACKAGE_PREFIX}-gettext")
source=("https://gnupg.org/ftp/gcrypt/libgpg-error/${_realname}-${pkgver}.tar.bz2"{,.sig}
        01-fix-broken-version.patch
        02-fix-symbollist-on.mingw.patch
        05-w32-gen.all.patch
        07-windows-build.patch
        10-gpg-error-static-linking.patch)
#These might be signed by any of these keys https://gnupg.org/signature_key.html
validpgpkeys=('5B80C5754298F0CB55D8ED6ABCEF7E294B092E28'  # Andre Heinecke (Release Signing Key)
              '6DAA6E64A76D2840571B4902528897B826403ADA'  # Werner Koch (dist signing 2020)
              'AC8E115BF73E2D8D47FA9908E98E9B2D19C6C8BD'  # Niibe Yutaka (GnuPG Release Key)
              '02F38DFF731FF97CB039A1DA549E695E905BA208') # GnuPG.com (Release Signing Key 2021)
sha256sums=('9e3c670966b96ecc746c28c2c419541e3bcb787d1a73930f5e5f5e1bcbbb9bdb'
            'SKIP'
            '4bfbd527f0b1e6c411f3b90fcb70180213ca2a247ee48c250916b30953718313'
            '118ae7c56ef6a3f57cbf834d06b17e3f6a9af4d5fa63fb07cdf2fe4a172c92be'
            'eff12bcd98ec928a99f12c6ce3118a71828e0dc0c753fb613266d4d737b1c23a'
            'a245bba8e6a115be451a83a76143c5a9ab86e536c7668c3cd1a9f535ab205b10'
            '9780e3e397b983ef52d5dae22d54c4453a47bddf04eafa2f874c75038e3485eb')

_apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -p1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  _apply_patch_with_msg \
    01-fix-broken-version.patch \
    02-fix-symbollist-on.mingw.patch \
    05-w32-gen.all.patch \
    07-windows-build.patch \
    10-gpg-error-static-linking.patch

  autoreconf -fi
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  ../${_realname}-${pkgver}/configure \
      --prefix=${MINGW_PREFIX} \
      --build=${MINGW_CHOST} \
      --host=${MINGW_CHOST} \
      --target=${MINGW_CHOST} \
      --enable-install-gpg-error-config \
      --enable-shared \
      --enable-static

  make
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  make DESTDIR="${pkgdir}" install

  install -Dm644 ${srcdir}/${_realname}-${pkgver}/COPYING ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/COPYING.LIB ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.LIB
}
